<template>
  <div>
    <ul class="tab-top">
      <li
        v-for="category in categories"
        :key="category.key"
        :class="{active: category.key === activeCategory}"
        @click="onTabClick(category.key)"
      >{{category.name}}</li>
    </ul>
    <ul class="list">
      <li
        class="listitem"
        v-for="item in currentCategories"
        :key="item.id"
      >
        <router-link tag="div" :to="`/detail/${item.id}`">
          <img
            v-lazy.container="item.poster.thumbnail"
            :v-lazy="item.poster.thumbnail"
            :alt="item.name">
          <div class="desc">
            <h2>{{item.name}}</h2>
            <p>{{item.intro}}</p>
            <p v-if="item.isNowPlaying">{{item.cinemaCount}}家影院正在热播</p>
            <div class="bottom-bar">
              <div class="grade">
                评分: <mt-badge size="small" type="warning">{{item.grade}} </mt-badge>
              </div>
              <mt-button
                @click.native.stop="addToCart({
                  id: item.id,
                  title: item.name,
                  url: item.poster.origin
                })"
                size="small" type="primary"
              >加入购物车</mt-button>
            </div>
          </div>
        </router-link>
      </li>
    </ul>
  </div>
</template>

<script>
import { getAllGoods } from '@/services';
import { mapMutations } from 'vuex';

export default {
  name: 'category',
  data() {
    return {
      list: [],
      categories: [{
        key: 'all',
        name: '全部',
      }, {
        key: 'promotion',
        name: '促销',
      }, {
        key: 'notPromotion',
        name: '非促销',
      }],
      activeCategory: 'all'
    };
  },
  mounted() {
    getAllGoods().then((resp) => {
      this.list = resp.films;
    });
  },
  computed: {
    currentCategories() {
      let ret = this.list
      switch(this.activeCategory) {
        case 'promotion':
          ret = this.list.filter(item => item.isPromotion === true);
          break;
        case 'notPromotion':
          ret = this.list.filter(item => item.isPromotion !== true);
          break;
        default:
          break;
      }
      return ret;
    },
  },
  methods: {
    ...mapMutations(['addToCart']),
    onTabClick(val) {
      this.activeCategory = val;
    },
  },
};
</script>

<style lang="scss" scoped>
  .tab-top {
    display: flex;
    list-style: none;
    li {
      line-height: 40px;
      background: #999;
      flex: 1;
      text-align: center;
      margin: 3px;

      &.active {
        background: #f00;
      }
    }
  }
  .listitem {
    height: 150px;
    padding: 15px;
    > div {
      display: flex;
      text-decoration: none;
      color: #666;
    }
    img {
      height: 150px;
    }
  }
  image[lazy=loading] {
    height: 150px;
    margin: auto;
  }
  .desc {
    margin-left: 15px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    h2 {
      font-size: 18px;
      color: #666;
    }
    p {
      font-size: 14px;
      color: #999;
    }
  }
  .bottom-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
